import Vue from 'vue'
import Vuex from 'vuex'
//安装插件
Vue.use(Vuex)
//创建对象
//Vuex的插件的store属性
const store = new Vuex.Store({
  state: { //保存状态
    counter: 1000,
    students: [
      { id: 1, name: 'Taec', age: '18' },
      { id: 2, name: '233', age: '56' },
      { id: 3, name: '444', age: '41' },
      { id: 4, name: '55', age: '21' },
    ],
    idol: {
      name: 'masaki',
      age: '35',
      address: 'Tok'
    }
  },
  mutations: { //定义方法（同步方法）
    increment(state) {  //这里有个默认的参数state，对应我们上面的state对象
      state.counter++;
    },
    decrement(state) {
      state.counter--;
    },
    incrementCount(state, playload) {
      state.counter += playload.num
    },
    UpdateInfo(state) {
      // setTimeout(() => {   //我们这里能正确修改，但是不能被工具追踪了
      state.idol.name = "nino"
      // }, 1000);

    }
  },
  actions: {
    // aUpdateInfo(context) { //这里也有个默认的参数context（上下文），这里可以理解为store
    //   setTimeout(() => {  
    //     context.commit('UpdateInfo')
    //   }, 1000);
    // },
    aUpdateInfo(context, payload) {
      return new Promise((resolve, reject) => {
        console.log(payload);
        setTimeout(() => {     
          context.commit('UpdateInfo')
          resolve('111111111')
        }, 1000);
      })
    }
  },
  getters: {  //类似计算属性，对状态进行操作
    powerCounter(state) { //有一个默认的state变量
      return state.counter * state.counter
    },
    more20stu(state) {
      return state.students.filter(s => s.age > 20);
    },
    more20stuLength(state, getters) {
      return getters.more20stu.length
    },
    Agemore(state) {
      return function (age) {
        return state.students.filter(s => s.age > age);
      }
    }
  },
  modules: {

  }
})
export default store